﻿using System;
using System.Collections.Generic;
using System.Web;
using FTchina.DAL;
namespace FTchina.Library
{
    /// <summary>
    ///MasterHelper 的摘要说明
    /// </summary>
    public class MasterHelper
    {
        public MasterHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        #region 验证是否有管理权限
        /// <summary>
        /// 获取管理权限
        /// </summary>
        /// <param name="UserPage">验证页面</param>
        /// <param name="Area">管辖区域</param>
        /// <returns></returns>
        public static void CheckLogin()
        {
            Logic.IPtableLogic.CheckUserIP();
            if (!MasterHelper.IsLogin)
            {
                FTchina.Logic.AlertLogic.ShowResultAndRedirect("请先登陆系统", "错误！", FineUI.MessageBoxIcon.Error, "/ui/login.aspx", true);

            }
        }


        /// <summary>
        /// 检测是否有权限执行相关代码
        /// </summary>
        /// <returns>true，false</returns>
        public static bool IsAdmin
        {//系统管理员权限
            get { return (MasterHelper.UserGroup == "1"); }
        }
        /// <summary>
        /// 检测是否有权限执行相关代码
        /// </summary>
        /// <returns>true，false</returns>
        public static void ChkPoewr(string pageurl)
        {
            string power = string.Empty;
            if (CacheHelper.IsExits("power" + pageurl+MasterHelper.UserGroup))
            {
                power = (string)CacheHelper.Get("power" + pageurl + MasterHelper.UserGroup);
            }
            else
            {
                power = DBHelper.GetValueByKey("power", "menulist", "link='" + pageurl + "'", "power" + pageurl + MasterHelper.UserGroup);
                CacheHelper.Insert("power" + pageurl, power, 3000);
            }
            //LogHelper.Debug("power" + pageurl + power + MasterHelper.UName + MasterHelper.UserGroupName);
            if (power.Length != 0)
            {
                if (!StringHelper.ChkISinArray(MasterHelper.UserGroup, StringHelper.String2ArrayList(power)))
                {
                    HttpContext.Current.Response.Write(MasterHelper.UName + "无权访问此页");
                    HttpContext.Current.Response.Write("<br/>请尝试将134.*.*.*添加到信任站点。");
                    HttpContext.Current.Response.End();
                }
            }
            //else
            //{
            //    LogHelper.Debug("控制外访问" + pageurl + MasterHelper.UName + MasterHelper.UserGroupName);
            //}
        }
        #endregion



        /// <summary>
        /// 判断用户是否登陆系统
        /// </summary>
        public static Boolean IsLogin
        {
            get { return (MasterID.Length != 0); }
        }

        /// <summary>
        /// 登录用户ID,每次登陆随机产生，MD5加密后保存
        /// </summary>
        public static string MasterID
        {
            get { return RequestHelper.GetCookie("master_id"); }
            set { RequestHelper.WriteCookie("master_id", value); }
        }


        /// <summary>
        /// 登陆用户姓名
        /// </summary>
        public static string UName
        {
            get { return RequestHelper.GetCookie("master_UName"); }
            set { RequestHelper.WriteCookie("master_UName", value); }
        }
        /// <summary>
        /// 登陆用户账户
        /// </summary>
        public static string UID
        {
            get { return RequestHelper.GetCookie("master_UID"); }
            set { RequestHelper.WriteCookie("master_UID", value); }
        }
        /// <summary>
        /// 登陆用户昵称
        /// </summary>
        public static string Nickname
        {
            get { return RequestHelper.GetCookie("master_Nickname"); }
            set { RequestHelper.WriteCookie("master_Nickname", value); }
        }

        /// <summary>
        /// 用户角色ID
        /// </summary>
        public static string UserGroup
        {
            get { return RequestHelper.GetCookie("master_UserGroup"); }
            set { RequestHelper.WriteCookie("master_UserGroup", value); }
        }
        /// <summary>
        /// 上级名称
        /// </summary>
        public static string ParentItemName
        {
            get { return RequestHelper.GetCookie("master_ParentItemName"); }
            set { RequestHelper.WriteCookie("master_ParentItemName", value); }
        }

        /// <summary>
        /// 用户角色名称
        /// </summary>
        public static string UserGroupName
        {
            get { return RequestHelper.GetCookie("master_UserGroupName"); }
            set { RequestHelper.WriteCookie("master_UserGroupName", value); }
        }
        /// <summary>
        /// 登陆用户进行操作记录ID
        /// </summary>
        public static string Luruid
        {
            get { return RequestHelper.GetCookie("master_Luruid"); }
            set { RequestHelper.WriteCookie("master_Luruid", value); }
        }



        /// <summary>
        /// 设置用户登录IP
        /// </summary>
        public static String LoginIP { get; set; }

        /// <summary>
        /// bssAreacode
        /// </summary>
        public static String BssAreaCode {
            get { return RequestHelper.GetCookie("master_BssAreaCode"); }
            set { RequestHelper.WriteCookie("master_BssAreaCode", value); }
        }

        /// <summary>
        /// AreaName，分公司名称
        /// </summary>
        public static String EssAreaName
        {
            get { return RequestHelper.GetCookie("master_EssAreaName"); }
            set { RequestHelper.WriteCookie("master_EssAreaName", value); }
        }
    }

}